package com.tusuo.stds.ui.level;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.View;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.tusuo.stds.Constants;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelDetail;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.integrity.IntegrityFragment_;
import com.tusuo.stds.ui.main.TabEntity;
import com.tusuo.stds.ui.scene.SceneActivity_;
import com.tusuo.stds.ui.scenedetail.SceneDetailActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_level)
public class LevelActivity extends BaseToolbarActivity<LevelView, LevelPresenter>
                                implements LevelView {

    @ViewById(R.id.level_tab)
    CommonTabLayout mTabLayout;

    @StringRes(R.string.toolbar_title_level)
    String toolbarTitle;

    private int mLevelId;
    private LevelDetail levelDetail;
    private SceneList.Scene mScene;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragmentList = new ArrayList<>();

    @AfterInject
    void init() {
        mLevelId = getIntent().getIntExtra("levelId", 0);
    }

    @AfterViews
    void afterViews() {
        setupUI(this);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config("", true, menuActionList);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public LevelPresenter createPresenter() {
        return new LevelPresenter(this, mLevelId);
    }

    @Override
    protected void onResume() {
        super.onResume();

        presenter.loadData();
    }

    @Override
    public void setLevelDetail(final LevelDetail levelDetail) {
        this.levelDetail = levelDetail;

        mToolbarTitle.setText(levelDetail.Title);

        if (mTabLayout.getTabCount() < 1) {
            mTabEntities = new ArrayList<>();
            mFragmentList = new ArrayList<>();
            mFragmentList.add(LevelFragment_.builder().levelDetail(levelDetail).build());

            String[] mTitles ;
            if (levelDetail.CompleteType == 2) {
                mTitles = new String[]{"关卡详情", "知识完整度"};
                mFragmentList.add(IntegrityFragment_.builder().mDataId(mLevelId).mType(Constants.CODE_COMPLETE_TYPE_LEVEL).build());
            } else {
                mTitles = new String[]{"关卡详情"};
                mTabLayout.setVisibility(View.GONE);
            }

            for (int i = 0; i < mTitles.length ; i ++) {
                mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
            }

            mTabLayout.setTabData(mTabEntities, this, R.id.level_content, mFragmentList);
        } else {
            LevelFragment_ levelFragment = (LevelFragment_) mFragmentList.get(0);
            levelFragment.levelDetail = levelDetail;
            levelFragment.initData();

            if (mFragmentList.size() > 1) {
                IntegrityFragment_ integrityFragment = (IntegrityFragment_) mFragmentList.get(1);
                integrityFragment.initData();
            }
        }
    }

    @Override
    public void jumpToSceneDetail() {
        Intent intent = new Intent();
        intent.setClass(LevelActivity.this, SceneDetailActivity_.class);
        intent.putExtra("sceneId",levelDetail.PreScene.Id);
        startActivity(intent);
    }

    @Override
    public void jumpToSceneList() {
        Intent intent = new Intent();
        intent.setClass(LevelActivity.this, SceneActivity_.class);
        intent.putExtra(SceneList.Scene.EXTRA_NAME, Parcels.wrap(levelDetail.PreScene));
        startActivity(intent);
    }

    public void toQuestionPage() {
        presenter.toQuestionPage(mLevelId);
    }

    public void isSceneUnlocked(int id) {
        presenter.isSceneUnlocked(id);
    }
}
